<template>
	<view>
		<form @reset="">
			<!-- 标题 -->
<!-- 				<view class="cu-form-group margin-top">
					<view class="title">标题</view>
					<input bindinput="getTitleValue" name="title"  placeholder="标题" v-model="title"></input>
				</view> -->
				<!-- end -->
				<!-- 内容 -->
					<view class="cu-form-group margin-top">
						<textarea  name="content"  maxlength="1000" v-model="content" placeholder="内容"></textarea>
					</view>
					<!-- end -->
					
					<!-- 图片 -->
					<view class="cu-bar bg-white margin-top">
							<view class="action">
								图片上传
							</view>
							<view class="action">
								{{imgList.length}}/5
							</view>
						</view>
						<view class="cu-form-group">
							<view class="grid col-4 grid-square flex-sub">
								<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
									<image :src="imgList[index]" mode='aspectFill'></image>
									<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
										<text class="cuIcon-close"></text>
									</view>
								</view>
								<view class="solids" @tap="ChooseImage" v-if="imgList.length < 1">
									<text class="cuIcon-cameraadd"></text>
								</view>
							</view>
						</view>
					<!-- end -->
					
					<!-- 地址选择 -->
<!-- 						<view class="cu-form-group">
							<view class="title">地址选择</view>
							<picker mode="multiSelector" @change="MultiChange" @columnchange="MultiColumnChange" :value="multiIndex"
							 :range="multiArray">
								<view class="picker">
									{{multiArray[0][multiIndex[0]]}}，{{multiArray[1][multiIndex[1]]}}，{{multiArray[2][multiIndex[2]]}}
								</view>
							</picker>
						</view> -->
					<!-- end -->
					
					<!-- 价钱 -->
<!-- 						<view class="cu-form-group margin-top">
							<view class="title">出售价:</view>
							<input type="digit" @input="moneyInput" :value="money" placeholder="请输入价钱"  maxlength='7' name="newPrice"></input>
					
							<view class="title">原价:</view>
							<input type="digit" @input="newInput" :value="newMoney" placeholder="请输入原价"  maxlength='7' name="oriPrice"></input>
					
						</view> -->
					<!-- end -->
					
					<!-- 选择分类  -->
						<view class="cu-form-group">
							<view class="title">分类:</view>
							<input disabled="true"  name="classify" :value='classify'></input>
							<button class="cu-btn  bg-green" role="button" aria-disabled="false" @tap="showModal" data-target="DrawerModalL">选择</button>
						</view>
					<!-- end -->
					
					<!-- 新旧 -->
<!-- 						<view class="cu-form-group">
							<view class="title">新旧:</view>
								<input disabled="true"  name="itemLists" :value='itemLists[itemListsIndex]'></input>
							<button class="cu-btn  bg-green" role="button" aria-disabled="false" @tap="newState">选择</button>
						</view> -->
					<!-- end -->
					
					<!-- 交易方式 -->
<!-- 					  <view class="cu-form-group">
							<view class="title">交易方式</view>
					<checkbox-group name="means" @change="checkboxChange">
					    <checkbox  :class="checkboxs[0].checked?'checked':'' " :checked="checkboxs[0].checked?true:false"  value="自提"  ></checkbox> 自提
					    <checkbox  :class="checkboxs[1].checked?'checked':'' " :checked="checkboxs[1].checked?true:false" 	value='同城面交'></checkbox> 同城面交
					    <checkbox  :class="checkboxs[2].checked?'checked':'' " :checked="checkboxs[2].checked?true:false"  value='邮寄'></checkbox> 邮寄
					</checkbox-group>
					
						</view> -->
					<!-- end -->
					
					<!-- 确定发布 -->
					<view class="padding flex flex-direction">
					  <button class="cu-btn bg-green margin-tb-sm lg"  form-type="submit" @tap="postContent">确定发布</button>
					</view>
					<!-- end -->
		</form>
		
		<!-- 模态框 -->
		<view  @touchmove.stop="modeMove" class=" cu-modal drawer-modal justify-start " :class=" modalName == 'DrawerModalL'?'show':'' " @tap="hideModal" >
		  <scroll-view scroll-with-animation='true' scroll-y='true' class="cu-dialog basis-lg" >
		    <view class="cu-list menu text-left">
		      <view class="cu-item " v-for=" (item,index) in  picker" :key="index" @tap="getClassify(picker[index])" :data-name="item.classify_name" :data-value="item.classify_id">
		        <view class="content">
		          <view>{{item.name}}</view>
		        </view>
		      </view>
		    </view>
		  </scroll-view >
		</view>
		
	</view>
</template>

<script>
	import store from '../../globalStore/store.json'
	import labels from '../../datas/labels.json'
	import getDate from '../component/getNowFormatDate.js'
	import posts from '../../datas/posts.json'
	import datas from '../../datas/data.json'
	export default {
		data() {
			return {
				modalName:'',//模态框开关
				 picker:[],
				    itemListsIndex:0,//几层新下标（默认全新）
					classify:'',//分类选择默认
					title:'',
					content:'',
					imgList: [],//图片上传
					up_imgs:[]

			}
		},
		onLoad() {
			this.picker=datas.labels.post_types;
			console.log(this.picker)
		},
		onShow() {

		},
		methods: {
		

			
			postContent:function(){

				let post={
					"post_id":Math.random().toString(36).substr(2),
					"user_name":store.user.user_name,
					"user_id":"q2",
					"icon":"https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg",
					"content":this.content,
					"imgs":this.up_imgs,
					"classify":this.classify,
					"time":getDate(),
					"praise_count":0,
					"comment_count":0,
					"comments":[]
				};
				console.log(post);
				let p=datas.posts;
				p.push(post);
				this.content="";
				this.imgList=[];
				this.classify="";
				this.up_imgs=[];
				
			},
			  // 图片上传
			ChooseImage:function() {
				uni.chooseImage({
				  count: 5, //默认9
				  sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				  sourceType: ['album'], //从相册选择
				  success: (res) => {
					  for(let i=0; i<res.tempFilePaths.length; i++){
						  uni.getFileSystemManager().readFile({
							filePath: res.tempFilePaths[i],
							encoding: 'base64',
							success: r => {				  		
								let tmp="data:image/png;base64,"+r.data;
								this.up_imgs.push(tmp);
							}
						  })					  
					  }

					if (this.imgList.length != 0) {
						this.imgList.concat(res.tempFilePaths)
					} else {
						this.imgList = res.tempFilePaths
					}
				  }
				});
			},
			ViewImage:function(e){
				uni.previewImage({
				  urls: this.imgList,
				  current: e.currentTarget.dataset.url
				});
			},
			  // 删除照片
			DelImg:function(e) {
				uni.showModal({
				  title: '提示',
				  content: '确定要删除这个照片吗？',
				  cancelText: '再看看',
				  confirmText: '删除',
				  success: res => {
					if (res.confirm) {
					  this.imgList.splice(e.currentTarget.dataset.index, 1);
					  this.up_imgs.splice(e.currentTarget.dataset.index, 1);
						this.imgList = this.imgList
					}
				  }
				})
			},
						  

				// 显示分类模态框
			showModal:function(e){
			  this.modalName = e.currentTarget.dataset.target
			},
				  // 隐藏分类模态框
			hideModal:function(e) {
			  this.modalName = null
			},
						    // 得到分类的值
			getClassify:function(label){
				this.classify = label.name,
				this.classify_id = label.id
			  this.hideModal();
			},

		},

		// 出发下拉刷新
		// onPullDownRefresh() {
		// 	// 模拟刷新完成
		// 	setTimeout(function(){
		// 		uni.stopPullDownRefresh();
		// 	},2000);
		// }
	}
</script>

<style>
.rule{
  display: flex;
  justify-content: space-between;
}
.margin-top-xl-170{
  margin-top: 170rpx;
}
</style>
